package com.koalac.ironman.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.Vector;

public class CalendarUtil {

	/**
	 * 获取日历日期
	 * 
	 * @param year
	 *            年 [1900,2100]
	 * @param month
	 *            月 如：[1,12]
	 * @param isFromSunday
	 *            是否从周日开始
	 * @param isFill
	 *            是否填充日历，显示上一个或下一个月的日期
	 * @return
	 */
	public static Vector<Integer> getDays(int year, int month,
			boolean isFromSunday, boolean isFill) {
		Vector<Integer> days = new Vector<Integer>();
		if (year < 1900 || month <= 0 || month >= 13) {
			return days;
		}
		Date date = new Date(year - 1900, month - 1, 1);
		int preCnt = date.getDay();
		if (!isFromSunday) {
			preCnt = (preCnt + 6) % 7;
		}
		// 获取上一个月补充的天数
		int preDays = getLastMonstCntOfDays(year, month);
		for (int i = 0; i < preCnt; i++) {
			if (isFill) {
				days.add(preDays - (preCnt - i));
			} else {
				days.add(0);
			}
		}
		// 装填这个月的日期
		int nowCnt = getCntOfDays(year, month);
		for (int i = 1; i <= nowCnt; i++) {
			days.add(i);
		}
		// 获取下一个月添加的天数
		int nextCnt = 0;
		if (days.size() <= 28) {// 特殊的二月
			nextCnt = 28 - days.size();
		} else if (days.size() > 35) {// 日子数超过35天的月份
			nextCnt = 42 - days.size();
		} else {// 日子数为35天
			nextCnt = 35 - days.size();
		}
		for (int i = 0; i < nextCnt; i++) {
			if (isFill) {
				days.add(i + 1);
			} else {
				days.add(0);
			}
		}
		return days;
	}

	/**
	 * 是否为润年
	 * 
	 * @param year
	 *            年:[1900,2100]
	 * @return
	 */
	private static boolean isLeap(int year) {
		if ((0 == year % 4 && 0 != year % 100) || 0 == year % 400) {
			return true;
		}
		return false;
	}

	/**
	 * 获取某一年某一月的天数
	 * 
	 * @param year
	 *            年:[1900,2100]
	 * @param month
	 *            月:[1,12]
	 * @return
	 */
	private static int getCntOfDays(int year, int month) {
		int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (isLeap(year) && 2 == month) {
			return 29;
		}
		return days[month - 1];
	}

	/**
	 * 获取某一月的上一个月的天数
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	private static int getLastMonstCntOfDays(int year, int month) {
		if (month == 1) {
			month = 12;
			year -= 1;
		}
		return CalendarUtil.getCntOfDays(year, month);
	}

	/**
	 * 获取当前天数
	 */
	public static int getCurDay() {
		return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取当前月份
	 * 
	 * @return 月份从1开始，一月则返回1
	 */
	public static int getCurMonth() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取当前年份
	 * 
	 * @return 年数
	 */
	public static int getCurYear() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 获取指定年、月的日期数据
	 * 
	 * @return 整型数组
	 */
	public static Integer[] getCalendar(int year, int month) {
		Vector<Integer> ver = CalendarUtil.getDays(year, month, true, false);

		Integer[] list = new Integer[ver.size()];
		ver.toArray(list);
		return list;
	}

	// 默认时间格式
	public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
	public static final String YMD_DATE_PATTERN = "yyyy-MM-dd";

	/**
	 * 字符串转为时间
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Date string2Date(String date, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.US);
		Date d = null;
		try {
			if (date == null || "".equals(date))
				return new Date();
			d = format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			d = new Date();
		}
		return d;
	}

	/**
	 * 字符串转为时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date string2Date(String date) {
		return string2Date(date, DEFAULT_DATE_PATTERN);
	}

	/**
	 * 时间转为字符串
	 * 
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss格式
	 */
	public static String date2String(Date date) {
		if (date == null)
			return null;
		SimpleDateFormat formatter = new SimpleDateFormat(DEFAULT_DATE_PATTERN,
				Locale.US);
		formatter.setTimeZone(TimeZone.getDefault());
		return formatter.format(date);
	}
	
	public static String timestamp2String(String timestamp){
		String dateString = null;  
		SimpleDateFormat sdf = new SimpleDateFormat(YMD_DATE_PATTERN);  
		long timeStampValue = Long.valueOf(timestamp);  
		dateString = sdf.format(new Date(timeStampValue * 1000L));  
		return dateString;  
	}

	/**
	 * 时间转为字符串
	 * 
	 * @param date
	 * @param pattern
	 *            时间样式
	 * @return
	 */
	public static String date2String(Date date, String pattern) {
		SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.US);
		formatter.setTimeZone(TimeZone.getDefault());
		return formatter.format(date);
	}
	
	
	/**
	 * 取得时间的描述，如刚刚、n分钟前……
	 * 
	 * @param date
	 * @return
	 */
	public static String getDateDesc(Date date) {
		Date today = new Date();
		long d1 = today.getTime();
		long d2 = date.getTime();
		long sub = (d1 - d2) / 1000;
		if (sub <= 60) {
			return "刚刚";
		} else if (sub > 60 && sub < 3600) {
			return (sub / 60) + "分钟前";
		} else if (sub >= 3600 && sub < 86400) {
			return (sub / 3600) + "小时前";
		} else if (sub >= 86400 && sub < 2592000) {
			return (sub / 86400) + "天前";
		} else if (sub >= 2592000 && sub < 15552000) {
			return (sub / 2592000) + "个月前";
		} else if (sub >= 15552000 && sub < 31104000) {
			return CalendarUtil.date2String(date, "MM-dd");
		}
		return CalendarUtil.date2String(date, "yyyy-MM-dd");
	}
}
